<?php ob_start(); ?>
<?php
$scriptWindowMsg = "
    $(function() {
        $('a#close').click(function() {
            window.close();
        });
    });";
$this->Html->scriptBlock($scriptWindowMsg, array('inline' => false));
$this->Html->scriptBlock($scriptFilter, array('inline' => false));
?>
<style>
    .title-tbl {
        background-color: #dedddd;
        font-size: 13px;
        font-weight: bold;
    }
    thead {
      background-color: #dedddd;
    }
    .td-payment-r {
        width: 100px;
    }
    td {
        text-align: center;
        vertical-align: middle;
        border-color: #333333;
        border-style: solid;
        border-width: 1px;
        height: 30px;
    }
    .tbl-broking-opentrade {
        border-left: none;
        border-top: none;
        height: auto;
        margin: 0;
        padding: 0;
        table-layout: fixed;
        width: 100%;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
    .clearfix:after {
        clear: both;
        content: ".";
        display: block;
        font-size: 0;
        height: 0;
        visibility: hidden;
    }
    .wrapper-table {
        height: auto;
        margin: 13px 0 0;
        padding: 0;
        width: 100%;
    }
    .clearfix {
        display: block;
    }
    .title-body-content {
        color: #A0002A;
        font-size: 17px;
        font-weight: bold;
        height: auto;
        margin: 0 0 0;
        padding: 0;
        text-align: center;
        width: 100%;
    }
    .wrapper-content-body {
        background-color: White;
        height: auto;
        margin: 0 0 60px;
        padding: 0 16px 0;
        width: 96.8%;
    }
    .wrapper-body {
        background-color: White;
        height: auto;
        margin: 0;
        padding: 10px 0 0;
        width: 100%;
    }
    .hightlight {
        background-color: yellow;
        font-weight: bold;
    }
    .line-content-tbl {
        font-size: 12px;
    }
    .td-payment {
        text-align: center;
        width: 150px;
    }
    .td-ref-payment {
        text-align: center;
        width: 200px;
    }
    .td-paid {
        text-align: center;
        width: 150px;
    }
    .td-payer {
        text-align: center;
        width: 200px;
    }
</style>
<div class="title-body-content"><?php echo $title_for_layout; ?></div>
<div class="wrapper-table clearfix">
    <?php 
        $HtmlCodetmp = ob_get_contents();
        ob_end_flush();
    ?>
    <div style="padding:10px 0">
        <b>Filter :</b>
        <select id="from_to_id" name="from_to">
            <option value="0" <?php echo $selected_from; ?>>Paid from</option>
            <option value="1" <?php echo $selected_to; ?>>Paid to</option>
        </select>
        <select id="outstanding_proccessed_id" name="outstanding_proccessed">
            <option value="">All</option>
            <option value="0" <?php echo $selected_outstanding; ?>>Outstanding</option>
            <option value="1" <?php echo $selected_proccessed; ?>>Processed</option>
        </select>
        <input type="hidden" value="<?php echo $ids; ?>" id="ids" />
    </div>
    
    <?php ob_start(); ?>
    
    <table class="tbl-broking-opentrade">
        <thead>
        <tr class="title-tbl" >
            <td class=""><?php echo $this->Paginator->sort('refid', 'Payment#'); ?></td>
            <td class=""><?php echo __('Reference#'); ?></td>
            <td class=""><?php echo ($to==1)? __('Payee') : __('Payer'); ?></td>
            <td class=""><?php echo __('Date'); ?></td>
            <td class=""><?php echo __('Amount'); ?></td>
            <td class=""><?php echo __('Processed'); ?></td>
        </tr>
        </thead>
        <?php
        $total = array();
        $totalUSD = 0;
        $count = count($paymentschedules);
        foreach ($paymentschedules AS $paymentschedule) {
            $date = '';
            $amount = 0;
            $checked = ($paymentschedule['Paymentschedule']['paid'] == '1') ? '&#10003;' : '';
            if($to==1) {
                $markets = json_decode($paymentschedule['Paymentschedule']['markets']);
                foreach ($markets as $market) {
                    if($market[2] != '0000-00-00') {
                        $date = date('m/d/Y', strtotime($market[2]));
                    }
                    if(isset($total[$paymentschedule['Paymentschedule']['currency']])){
                        $total[$paymentschedule['Paymentschedule']['currency']] += $market[3];
                    } else {
                        $total[$paymentschedule['Paymentschedule']['currency']] = $market[3];
                    }
                    $amount = number_format($market[3], 2);
                    echo '
                        <tr class="line-content-tbl" >
                            <td>' . $paymentschedule['Paymentschedule']['refid'] . '</td>
                            <td>' . $paymentschedule['Paymentschedule']['refid_edit'] . '</td>
                            <td>' . $market[0] . '</td>
                            <td>' . $date . '</td>
                            <td style="text-align:right; padding-right:5px; background-color: yellow;">' . $paymentschedule['Paymentschedule']['currency'] . ' ' . $amount . '</td>
                            <td>' . $checked . '</td>
                        </tr>';
                    $date = '';
                }
                
            } else {
                if($paymentschedule['Paymentschedule']['broker_date'] != '0000-00-00') {
                    $date = date('m/d/Y', strtotime($paymentschedule['Paymentschedule']['broker_date']));
                }
                if(isset($total[$paymentschedule['Paymentschedule']['currency']])){
                    $total[$paymentschedule['Paymentschedule']['currency']] += $paymentschedule['Paymentschedule']['broker_amount'];
                } else {
                    $total[$paymentschedule['Paymentschedule']['currency']] = $paymentschedule['Paymentschedule']['broker_amount'];
                }
                $amount = number_format($paymentschedule['Paymentschedule']['broker_amount'], 2);
                echo '
                    <tr class="line-content-tbl" >
                        <td>' . $paymentschedule['Paymentschedule']['refid'] . '</td>
                        <td>' . $paymentschedule['Paymentschedule']['refid_edit'] . '</td>
                        <td>' . $paymentschedule['Paymentschedule']['payer_name'] . '</td>
                        <td>' . $date . '</td>
                        <td style="text-align:right; padding-right:5px; background-color: yellow;">' . $paymentschedule['Paymentschedule']['currency'] . ' ' . $amount . '</td>
                        <td>' . $checked . '</td>
                    </tr>';
                $date = '';
            }
        }
        ?>
        <?php if($count>0) { ?>
        <tr class="line-content-tbl" >
            <td colspan="4" style="text-align:right; padding-right:5px; border: none;">
                <b>Total : </b>
            </td>
            <td colspan="1" style="text-align:right; padding-right:5px; border: none;">
                <b>
                <?php
                if($total!=null) {
                    foreach ($total as $key => $value){
                        echo $key . ' ' . number_format($value, 2) . '<br />';
                    }
                }
                ?>
                </b>
            </td>
            <td style="text-align:left; padding-right:5px; border: none;"></td>
        </tr>
        <?php } ?>
    </table>
</div>
<?php
    $HtmlCode = ob_get_contents();
    $HtmlCode = $HtmlCodetmp . $HtmlCode;
    ob_end_flush();
    $file = '/home/johnso28/public_html/aihreinsurance.com/dompdf/aih/open_trade_report.html';
    $HtmlCode1 = str_replace('&#10003;', '<img src="img/tick.gif" alt="" />', $HtmlCode);
    file_put_contents($file, $HtmlCode1);
?>

<div class="wrapper-button-form clearfix">
    <div class="button-form clearfix">
        <div class="button-aihre button-blue"><a href="JavaScript:void(0)" title="CLOSE" id="close"><?php echo __('CLOSE'); ?></a></div>
    </div>
</div>
<div class="wrapper-button-form clearfix">
    <div class="button-form clearfix">
        <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/dompdf/dompdf.php?base_path=aih/&input_file=open_trade_report.html&paper=a4">Download PDF</a>
        &nbsp; | &nbsp;
        <?php echo $this->Html->link('Download Excel', '/phpexcel/opentradeaccount.php?ids=' . $ids . $link_to . $link_proccessed, null, array()); ?>
    </div>
</div>